<template>
	<view class="container">
		<image src="../../static/logo.png" style="width: 288rpx; height: 288rpx; margin: 62rpx 232rpx 168rpx 232rpx"
			mode=""></image>
		<!-- <button class="btn" open-type="getPhoneNumber" @getphonenumber="phonelogin">授权手机号登录</button> -->
		<!-- 测试域名 -->
		<view class="form">

			<view class="title">手机号</view>
			<input v-model="mobile" class="ipt" type="number" placeholder="请输入手机号">

			<view class="title">验证码</view>
			<view style="position: relative;">
				<input v-model="captcha" class="ipt" type="password" placeholder="请输入密码">
				<view @click="gotcode" style="position: absolute;right: 150rpx;top: 20rpx;z-index: 99;">
					{{time == 60 ?  "获取验证码" :  time + '后重新获取'}}
				</view>
			</view>

			<view class="btn" @click="loginfn1">登录</view>

		</view>
		<view style="display: flex; align-items: center;  box-sizing: border-box; padding: 30rpx ;margin-top: 50rpx;">
			<radio style="transform: scale(0.7)" color="#28b567" :checked="check" @click="changestatus" />
			<view style="font-weight: 500; font-size: 24rpx; color: #333333">
				注册登录即表示同意
				<text style="color: #0088ff" @click="goxieyi(1)">《用户协议》</text>
				和
				<text style="color: #0088ff" @click="goxieyi(2)">《隐私政策》</text>
				并授权闪 验获取本机号码
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				swich: true,
				time: 60,
				check: false,
				mobile: "",
				captcha: ""
			};
		},
		methods: {
			loginfn1() {
				if (!/^1[3-9][0-9]{9}$/.test(this.mobile)) {
					return uni.showToast({
						title: '请输入正确手机号',
						icon: "none"
					})
				}
				if (this.captcha == '') {
					return uni.showToast({
						title: '请输入验证码',
						icon: "none"
					})
				}
				this.http('/user/mobilelogin', {
					mobile: this.mobile,
					captcha: this.captcha
				}).then(res => {
					console.log(res);
					this.$store.commit('setuser', res.data);
					this.$store.commit('settoken', res.data.token);
					uni.setStorageSync('rong_token', res.data.rong_token);
					if (res.code == 1) {
						this.utilim.connect(res.data.rong_token);
						// 获取用户信息判断是否注册
						this.http('/user/infor').then((res2) => {
							console.log('用户信息', res2);
							if (res2.data.birthday) {
								uni.redirectTo({
									url: '/pages/index/index'
								});
							} else {
								uni.navigateTo({
									url: '/login/choosesex/choosesex'
								});
							}
						});
					}
				})
			},

			// 获取验证码
			gotcode() {
				console.log(1111);
				if (this.swich) {
					console.log(11111);
					this.swich = false
					this.time--
					this.timer = setInterval(() => {
						this.time--
						if (this.time == 0) {
							this.swich = true
							this.time = 60
							clearInterval(this.timer)
							this.timer = null
						}
					}, 1000)
				} else {
					return uni.showToast({
						title: '请勿重复点击',
						icon: "none"
					})
				}
			},
			changestatus() {
				this.check = !this.check;
			},
			// 手机号登录
			phonelogin(e) {
				console.log('手机号code', e.detail.code);
				if (!this.check) {
					return uni.showToast({
						title: '请先阅读协议',
						icon: 'none'
					});
				}
				uni.login({
					success: (res) => {
						console.log(res.code);
						this.http('/user/weChatMiniLogin', {
							code: res.code,
							mobile_code: e.detail.code
						}).then((res2) => {
							console.log('登录结果', res2);
							// uni.setStorageSync('token', res2.data.token);
							this.$store.commit('setuser', res2.data);
							this.$store.commit('settoken', res2.data.token);
							uni.setStorageSync('rong_token', res2.data.rong_token);
							if (res2.code == 1) {
								this.utilim.connect(res2.data.rong_token);
								// 获取用户信息判断是否注册
								this.http('/user/infor').then((res) => {
									console.log('用户信息', res);
									if (res.data.birthday) {
										uni.redirectTo({
											url: '/pages/index/index'
										});
									} else {
										uni.navigateTo({
											url: '/login/choosesex/choosesex'
										});
									}
								});
							} else {
								uni.showToast({
									url: '登录失败'
								});
							}
						});
					}
				});
			},
			// 跳转隐私协议
			goxieyi(index) {
				uni.navigateTo({
					url: `/my/agreement/agreement?type=${index}`
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		.form {
			margin-top: 100rpx;

			.btn {
				width: 600rpx;
				height: 90rpx;
				background: linear-gradient(0deg, #28b567 6%, #5aca89 100%);
				border-radius: 14rpx;
				text-align: center;
				line-height: 90rpx;
				font-size: 30rpx;
				color: #ffffff;
				margin: 0 auto;
				margin-top: 100rpx;
			}

			.title {
				width: 600rpx;
				font-size: 28rpx;
				font-weight: 500;
				color: #333333;
				text-align: left;
				margin: 0 auto;
			}

			.ipt {
				width: 600rpx;
				height: 90rpx;
				background: #f6f6f6;
				border-radius: 10rpx;
				padding-left: 33rpx;
				box-sizing: border-box;
				text-align: left;
				font-size: 28rpx;
				margin: 0 auto;
				margin-top: 24rpx;
				margin-bottom: 30rpx;
			}
		}

		.btn {
			width: 670rpx;
			height: 100rpx;
			font-weight: 500;
			font-size: 28rpx;
			margin-left: 40rpx;
			color: #151515;
			background: #feff01;
			text-align: center;
			line-height: 100rpx;
			border-radius: 100rpx 100rpx 100rpx 100rpx;
		}
	}
</style>